package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamster.crm.exception.ExceptionEnum;
import com.hamster.crm.exception.LyException;
import com.hamster.crm.mapper.EmployeeMapper;
import com.hamster.crm.mapper.EmployeeScoreMapper;
import com.hamster.crm.mapper.ManagerGradeMapper;
import com.hamster.crm.pojo.Employee;
import com.hamster.crm.pojo.EmployeeScore;
import com.hamster.crm.pojo.ManagerGrade;

import com.hamster.crm.service.EmployeeScoreService;
import com.hamster.crm.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 员工评分记录Service
 * @author Tarry
 * @create 2019/12/21 16:05
 */
@Service
public class EmployeeScoreServiceImpl implements EmployeeScoreService {
    @Autowired
    private EmployeeScoreMapper employeeScoreMapper;
    @Autowired
    private ManagerGradeMapper managerGradeMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 为多个员工提交当月评分
     * @param scoreList
     */
    @Transactional
    @Override
    public void insert(List<EmployeeScore> scoreList) {
        Date date = new Date(); //当前系统时间
        String inputUser = "功能待完善"; //当前操作人

        for (EmployeeScore employeeScoreDTO : scoreList) {
            employeeScoreDTO.setScoreTime(date); //评分时间
            employeeScoreDTO.setDel("0");  //设置记录状态 为正常
            employeeScoreDTO.setInputTime(date);  //操作时间
            employeeScoreDTO.setInputUser(inputUser);//操作人
            //根据部门、分数，查询对应级别，并设置。
            String gradeTitle = this.queryGradeTitle(employeeScoreDTO.getDepartment(), employeeScoreDTO.getScore());
            employeeScoreDTO.setGrade(gradeTitle);
            employeeScoreMapper.insert(employeeScoreDTO);
        }
    }

    /**
     * 根据部门和分数查询，对应级别名称  部门id  1开发部  2服务部 3 事业部 4 财务部 5 后勤部
     * @return  职位名称
     */
    private String queryGradeTitle(String departmentId,Double score){
        //TODO
        String gradeTitle = null;

        if("2".equals(departmentId)){ //服务部
            QueryWrapper<ManagerGrade> queryWrapper = new QueryWrapper<>();
            //设置大小区间 左闭右开
            queryWrapper.eq("del","0").eq("department","2").le("min_grade",score).ge("max_grade",score);
            ManagerGrade managerGrade = managerGradeMapper.selectOne(queryWrapper);
            if (managerGrade == null){
                throw new LyException(ExceptionEnum.SCORE_NOT_IN_BETWEEN);
            }
            gradeTitle = managerGrade.getPosition();

        } else if ("3".equals(departmentId)){ //事业部
            QueryWrapper<ManagerGrade> queryWrapper = new QueryWrapper<>();
            //设置大小区间 左闭右开
            queryWrapper.eq("del","0").eq("department","3").le("min_grade",score).ge("max_grade",score);
            ManagerGrade managerGrade = managerGradeMapper.selectOne(queryWrapper);
            if (managerGrade == null){
                throw new LyException(ExceptionEnum.SCORE_NOT_IN_BETWEEN);
            }
            gradeTitle = managerGrade.getPosition();
        }

        return gradeTitle;
    }

    /**
     * 获取一个员工的评分历史记录  降序
     * @param id 员工Id
     * @return List<EmployeeScore>
     */
    @Override
    public List<EmployeeScore> list(String id) {
        QueryWrapper<EmployeeScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id",id).orderByDesc("score_time");
       return employeeScoreMapper.selectList(queryWrapper);
    }

    /**
     * 获取一个员工的最新评分 级别 和 评分时间  【列表中展示的内容，针对经理级别，前台单独调用】
     * @param id 员工Id
     * @return List<EmployeeScore>
     */
    @Override
    public EmployeeScore get(String id) {
//        QueryWrapper<EmployeeScore> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("employee_id",id).orderByDesc("score_time");
//
        return employeeScoreMapper.selectFirst(id);
    }

    /**
     * 查询 服务部和事业部员工
     * @param pageSize
     * @param currentPage
     * @param name
     * @param startTime
     * @param endTime
     * @param department
     * @return
     */
    @Override
    public PageResult<Employee> employeeList(Integer pageSize, Integer currentPage, String name, Date startTime, Date endTime, String department) {
        //分页
        Page<Employee> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("del","0").orderByDesc("join_time");

        //根据姓名过滤
        if (StringUtils.isNotBlank(name)){
            wrapper.like("name","%"+name+"%");

        }
        //根据招聘时间过滤
        if (startTime != null && endTime != null){
            wrapper.ge("join_time",startTime);
            wrapper.le("join_time",endTime);
        }
        //根据部门过滤
        if (StringUtils.isNotBlank(department)){
            wrapper.eq("department",department);
        }else{
            wrapper.in("department","2","3");
        }


        IPage pageList = employeeMapper.selectPage(page, wrapper);
        Long pages = pageList.getPages();
        List<Employee> currentList = pageList.getRecords();
        if (currentList != null){
            for (Employee employee : currentList) {
                EmployeeScore employeeScore = this.get(employee.getId());

                if (employeeScore != null){
                    employee.setScore(employeeScore.getScore());
                    employee.setScoreDate(employeeScore.getScoreTime());
                    employee.setJobLevel(employeeScore.getGrade());
                }

            }
        }


        return new PageResult<Employee>(pageList.getTotal(),pages.intValue(),currentList);
    }


}
